<?php

namespace app\models;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "bf_askfor".
 *
 * @property int $id
 * @property string|null $store_name 客户名称
 * @property int|null $info_id 公司ID
 * @property string|null $legal 法人
 * @property string|null $phone 账号
 * @property string|null $type 客户类型
 * @property float|null $caigou 过去30天采购额
 * @property int|null $bf_status 状态:1发起申请，2申请冻结，3申请封禁，-1审核失败，-2已冻结，-3已封禁，-4已驳回
 * @property float|null $moneyof 审批额度
 * @property int|null $hk_mode 还款方式：1自主还款，2云途养护代付
 * @property int|null $hk_time 还款日期
 * @property string|null $reason 审核原因
 * @property string|null $remark 审核说明
 * @property string|null $created_at 申请时间
 * @property string|null $updated_at 操作时间
 * @property string|null $notes 操作记录
 */
class Askfor
{
    //参数：
    //store_id  店铺ID
    //infor_id  公司ID
    //aid       申请ID
    //bf_status 用户状态
    public function userinfo($post)
    {
        if(!empty($post['infor_id'])){
            //公共部分，查找本地库db对应公司信息,没有即查询DB2
            $usercompany=Yii::$app->db->createCommand('select * from bf_infor where id=:id')
                    ->bindValue(':id', $post['infor_id'])
                    ->queryOne();
            if(empty($usercompany)){
                $usercompany=Yii::$app->db2->createCommand('select * from yt_trade_infor where id=:id')
                    ->bindValue(':id', $post['infor_id'])
                    ->queryOne();
            }
            if($post['bf_status']!=0 && $post['bf_status']!='-1'){
                $list=(new Query())
                    ->select('*')
                    ->from('bf_customer')
                    ->where('id=:id')
                    ->addParams([':id'=>$post['store_id']])
                    ->one();
                $dflist=(new Query())
                    ->select('*')
                    ->from('bf_repay')
                    ->where('store_id=:store_id')
                    ->addParams([':store_id'=>$post['store_id']])
                    ->all();
            }else{
                $list=(new Query())
                    ->select('*')
                    ->from('bf_askfor')
                    ->where('id=:id')
                    ->addParams([':id'=>$post['aid']])
                    ->one();
            }
            $date=[
                'company'=>$usercompany,
                'list'=>$list ? $list:'',
                'dflist'=>$dflist ? $dflist:''
            ];

            return $date;
        }

    }

    //审批通过
    public function getpast($post)
    {
        //status：1通过-1未通过
        //审核通过修改DB申请表状态=1，新增公司数据，客户表数据，操作记录,
        //修改DB2客户表状态
        if(!empty($post['credit_limit']) && !empty($post['repay_date'])){
            $transaction = Yii::$app->db->beginTransaction();
            try {
                //查询公司信息，插入DB
                $usercompany=Yii::$app->db2->createCommand('select * from yt_trade_infor where id='.$post['infor_id'])
                    ->bindValue(':id', $post['infor_id'])
                    ->queryOne();
               if(!empty($usercompany)){
                   unset($usercompany['id']);
                   $usercompany['business_form']=1;//1保付用户
                   $db_insertcompany=Yii::$app->db->createCommand()->insert('bf_infor', $usercompany)->execute();
                   $post['infor_id']=Yii::$app->db->getLastInsertID();
                   $post['passing_time']=date("Y-m-d H:i:s");
                   $post['passing_note']='账户审核通过';
                   $db_insertcustomer=Yii::$app->db->createCommand()->insert('bf_customer', [
                       'infor_id'=>$post['infor_id'],
                       'store_name'=>$post['store_name'],
                       'credit_limit'=>$post['credit_limit'],
                       'repay_date'=>$post['repay_date']
                   ])->execute();
                   $db_update=Yii::$app->db->createCommand('UPDATE bf_askfor SET bf_status=1 WHERE id='.$post['aid'])->execute();
                   $db2_update=Yii::$app->db2->createCommand('UPDATE yt_trade_store SET bf_status=1 WHERE id='.$post['store_id'])->execute();
                   $transaction->commit();
               }

            } catch (\Exception $e) {
                $transaction->rollBack();
            }
        }
    }

    //审批不通过
    public function getfailed($post)
    {
        //审核未通过,更新DB申请记录,更新DB2用户状态
        //参数：status-1不通过
        //     store_id  申请中关联DB2的店铺ID
        //     aid   申请ID
        $notes=Yii::$app->request->post();
        $db_update = Yii::$app->db->createCommand()->update('bf_askfor', [
            'reason' => $notes['reason'],
            'remark' => $notes['remark'],
            'bf_status' =>'-1',
        ], "id=:id", [
            ':id' => $post['aid']
        ]);
        $db2_update=Yii::$app->db2->createCommand('UPDATE yt_trade_store SET bf_status='.$post['status']. 'WHERE id='.$post['store_id'])->execute();
    }

    //冻结,封禁
    public function closeuser($post)
    {
        //type:2冻结3封禁
        if(!empty($post)){
            if($post['type']=='1'){
                $transaction = Yii::$app->db->beginTransaction();
                try {
                    $db_update=Yii::$app->db->createCommand('UPDATE bf_customer SET status='.$post['status'].' WHERE id='.$post['id'])->execute();
                    $db2_update=Yii::$app->db2->createCommand('UPDATE yt_trade_store SET bf_status='.$post['status'].' WHERE id='.$post['id'])->execute();
                } catch (\Exception $e) {
                    $transaction->rollBack();
                }
            }elseif ($post['type']=='-1'){
                $db_update = Yii::$app->db->createCommand()->update('bf_askfor', [
//                    'reason' => $notes['reason'],
//                    'remark' => $notes['remark'],
                    'bf_status' =>$post['status'],
                ], "id=:id", [
                    ':id' => $post['aid']
                ]);
                $db2_update=Yii::$app->db2->createCommand('UPDATE yt_trade_store SET bf_status='.$post['status']. 'WHERE id='.$post['store_id'])->execute();
            }

        }
    }

}
